<?php
/* 
 * Copyright (c) 2018-2021   All rights reserved.
 * 
 * 创建时间：2021-08-30 17:18
 *
 * 项目：rm  -  $  - MyController.php
 *
 * 作者：liwei 
 */

namespace modules\levpays\controllers;

use Lev;
use lev\base\Controllerv;
use lev\base\Viewv;
use lev\helpers\ModulesHelper;
use lev\helpers\ScoreHelper;
use lev\helpers\UserHelper;
use modules\levpays\sdk\wechatpay\InstancePay;
use modules\levpays\table\tradesHelper;

!defined('INLEV') && exit('Access Denied LEV');


class MyController extends Controllerv
{

    public static function actionIndex() {

        $iden = Lev::stripTags(Lev::GPv('iden')) ?: null;

        $subnavs['allTrades'] = [
            'id'     => 'allTrades',
            'tab'    => 'common',
            'icon'   => '#fa-list',
            'name'   => '全部',
            'url'    => Lev::toReRoute(['my/ajax-trades-list', 'iden'=>$iden]),
        ];

        $subnavs['payed'] = [
            'id'     => 'payed',
            'tab'    => 'common',
            'icon'   => '#fa-list',
            'name'   => '已支付',
            'url'    => Lev::toReRoute(['my/ajax-trades-list', 'status'=>1, 'iden'=>$iden]),
        ];

        $subnavs['nopay'] = [
            'id'     => 'nopay',
            'tab'    => 'common',
            'icon'   => '#fa-list',
            'name'   => '未支付',
            'url'    => Lev::toReRoute(['my/ajax-trades-list', 'status'=>0, 'iden'=>$iden]),
        ];

        $deTabId = 'allTrades';

        $mudInfo = ModulesHelper::getModuleFileInfo($iden);
        $name = $mudInfo ? $mudInfo['name'] : '';
        Lev::$app['title'] = '我的'.$name.'订单';

        Viewv::render('my/index', [
            'deTabId' => $deTabId,
            'subnavs' => $subnavs,
            'myScores'=> ScoreHelper::myScores(),
        ]);

    }

    public static function actionAjaxTradesList() {

        if (Lev::$app['uid'] <1) {
            $htms = '';
            $not = 1;
        }else {
            $order = ['id DESC'];
            $limit = 10;

            $where['uid'] = Lev::$app['uid'];
            is_numeric($status = Lev::GPv('status')) && $where['tradestatus'] = $status;
            ($iden = Lev::stripTags(Lev::GPv('iden'))) && $where['iden'] = $iden;

            $where['status'] = 0;

            $lists = tradesHelper::pages($where, $limit, $order, '*');
            $not = count($lists) < $limit ? 1 : 0;

            $htms = empty($lists) ? '' : Viewv::renderPartial('my/ajax_trades_list', [
                'lists' => $lists,
                'paytypeidIcons' => tradesHelper::paytypeidIcon(),
                'tradestatuses' => tradesHelper::tradestatus(),
            ]);
        }
        echo json_encode(Lev::responseMsg(1, '', ['htms'=>$htms, 'not'=>$not]));
    }

    public static function actionTradeCheck() {
        parent::csrfValidation();

        echo json_encode(static::TradeCheck());
    }
    public static function actionTradeStatus() {
        parent::csrfValidation();

        echo json_encode(static::TradeStatus());
    }
    public static function TradeStatus() {
        if (Lev::$app['uid'] <1) {
            return Lev::responseMsg(-5, '');
        }
        $opid = floatval(Lev::GPv('opid'));
        $ck = tradesHelper::findOne(['id'=>$opid]);
        if (empty($ck) || $ck['status']) {
            return Lev::responseMsg(-202, '查无订单');
        }
        if ($ck['tradestatus']) {
            return Lev::responseMsg(-203, '订单状态异常');
        }
        if ($ck['uid'] != Lev::$app['uid']) {
            return Lev::responseMsg(-204, '非法操作他人订单');
        }
        if ($ck['addtime'] > Lev::$app['timestamp'] - 3000) {
            return Lev::responseMsg(-205, '新下单子需要半小时后才可放弃');
        }
        tradesHelper::update(['tradestatus'=>4], ['id'=>$opid]);
        return Lev::responseMsg();
    }

    public static function TradeCheck()
    {
        if (Lev::$app['uid'] <1) {
            return Lev::responseMsg(-5, '');
        }
        $opid = floatval(Lev::GPv('opid'));
        $ck = tradesHelper::findOne(['id'=>$opid]);
        if (empty($ck) || $ck['status']) {
            return Lev::responseMsg(-302, '查无订单');
        }
        if ($ck['tradestatus'] == 1) {
            return Lev::responseMsg(-303, '订单状态异常');
        }
        if ($ck['uid'] != Lev::$app['uid']) {
            return Lev::responseMsg(-304, '非法操作他人订单');
        }
        $tradeNo = tradesHelper::getTradeno($opid);
        $pre = end(json_decode($ck['paymethod'], true));
        !$pre && $pre = $ck['paysource'];
        $res = InstancePay::checkTradeNo(InstancePay::getOutTradeNo($tradeNo, $pre));
        if ($res['_paySuccess']) {
            InstancePay::paySuccess(['message'=>'已支付'], $tradeNo, $pre, $res);
            return Lev::responseMsg(1, $res['message'].'-'.$res['trade_state_desc']);
        }
        return Lev::responseMsg(-305, $res['message'].'-'.$res['trade_state_desc']);
    }
}